using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;
using System.Linq.Expressions;

namespace ShopPowerTemplate.Services
{
    public class ReviewService : ReviewServiceBase, IReviewService
    {
		public ReviewService(IEntitiesDB entitiesDB) : base(entitiesDB) { }

		//Custom code here

        public override List<Review> Search(string searchValue, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize = 10)
        {
            IQueryable<Review> query = EntitiesDB.ReviewSet.Include("Customer").Where(x => string.IsNullOrEmpty(searchValue) || x.Comment.Contains(searchValue));

            if (orderBy == string.Empty)
                orderBy = "Id";

            query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public override List<Review> Search(Expression<Func<Review, bool>> searchCriteria, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize = 10)
        {
            IQueryable<Review> query = EntitiesDB.ReviewSet.Include("Customer").Where(searchCriteria);

            if (orderBy == string.Empty)
                orderBy = "Id";

            query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }
	}
}	

